<?php
Globals::requireClass('Controller');
Globals::requireModel('User');
Globals::requireTable('User');
Globals::requireTable('Msg');
Globals::requireTable('Pushuser');

class ChatController extends Controller
{
    protected $UserTable;
	protected $MsgTable;
	protected $PushuserTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> true,
		'layoutEnabled'	=> true,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
		parent::__construct($config);
		$this->UserTable = new UserTable($config);
		$this->MsgTable = new MsgTable($config);
		$this->PushuserTable = new PushuserTable($config);
		dolog(json_encode($_GET));
	}
	
	public function indexAction()
	{
		$this->layout->pageTitle	= "test";
	}
	
	//jpush推送
	public function chatjpushAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-3'));
	        exit();
	    }
	    
	    $pushuid = $this->getParam('pushuid');
	    $verifypushuid = $this->getParam('verifypushuid');
	    $new_num_arr = array();
	    for ($ii = 1 ; $ii < (strlen($verifypushuid)-1) ; $ii++){
	        $tmp_num = substr($verifypushuid, $ii , 1);
	        $new_num_arr[] = $tmp_num;
	    }
	    krsort($new_num_arr);
	    $new_num = '';
	    foreach ($new_num_arr as $v){
	        $new_num .= (string)$v;
	    }
	    $new_num = (int)$new_num - 539718743;
	    if($new_num != $pushuid){
	        echo json_encode(array('returncode'=>'-4'));
	        exit();
	    }
	    
	    $hour = date('H');
	    if($hour >= 7 && $hour < 23){
	    if($uid != 1374 && $uid != 1377){
	        $pushuser = $this->PushuserTable->getRow(array('uid'=>$uid,'pushdate'=>date('Y-m-d')));
	        if($pushuser['id'] < 1){
	            $push_user_data['uid'] = $uid;
	            $push_user_data['pushdate'] = date('Y-m-d');
	            $push_user_data['count_number'] = 1;
	            $this->PushuserTable->add($push_user_data);
	        }else{
	            if($pushuser['count_number'] > 100){
	                echo json_encode(array('returncode'=>'-5'));
	                exit();
	            }else{
	                $tmp_count = $pushuser['count_number'] + 1;
	                $this->PushuserTable->update(array('count_number'=>$tmp_count),$pushuser['id']);
	            }
	        }
	    }
	    
	    $alert = $this->getParam('alert');
	    $content = $this->getParam('content');
	    $customfield = $this->getParam('customfield');
	    $customfield = str_replace('*', '&', $customfield);
	    $pushtype = $this->getParam('pushtype');
	    $type = $this->getIntParam('type');
	    
	    if($type == 1){
	        $msg_data['createdate'] = date('Y-m-d H:i:s');
	        $msg_data['uid'] = $pushuid;
	        $msg_data['msg'] = $content;
	        $msg_data['status'] = 0;
	        $this->MsgTable->add($msg_data);
	    }
	    }else{
	        $content = "实在不好意思，客服已下线，客服服务时间早7:00到晚11:00\n如有其他疑问请添加微信客服微信号:xiaoshulinapp";
	        $customfield = $this->getParam('customfield');
	        $customfield = str_replace('*', '&', $customfield);
	        $pushtype = $this->getParam('pushtype');
	        $alert = $this->getParam('alert');
	        $pushuid = $uid;
	    }
	    //$pushid = count($pushid) < 2 ? $pushid[0] : $pushid;
	    $exreas = array('content'=>$content,'customfield'=>$customfield,'pushtype'=>$pushtype);
	    $re = do_jpush($pushuid,$alert,$exreas);
	    if($re == 1){
	        echo json_encode(array('returncode'=>'0'));
	    }elseif($re == -1){
	        echo json_encode(array('returncode'=>'-1'));
	    }elseif($re == -2){
	        echo json_encode(array('returncode'=>'-2'));
	    }
	    
	    exit();
	}
	
	protected function out()
	{
		$this->layout->nav		= 'index';
		parent::out();
	}
}

Config::extend('ChatController', 'Controller');
